#include <iostream>
#include <SFML/Network.hpp>

#include "RNetwork.h"

RNetwork::RNetwork(const char *ip, unsigned short port) : _server(ip), _port(port)
{
	std::cout << "Trying to connect ..." << std::endl;
	if (this->_server.IsValid() == true)
		std::cout << "Server found." << std::endl;
	else
		std::cout << "Server can not be reach at this time. Try later." << std::endl;

	if (this->_client.Bind(port) != true)
		std::cout << "Error Bind Socket" << std::endl;
}

RNetwork::~RNetwork()
{
	this->_client.Close();
}

void	RNetwork::sendToServer(sf::Packet &message)
{
	if (this->_client.Send(message, this->_server, this->_port) != sf::Socket::Done)
		std::cout << "Error sending message" << std::endl;
}

void	RNetwork::rcvFromServer(sf::Packet &message)
{
	if (this->_client.Receive(message, this->_server, this->_port) != sf::Socket::Done)
		std::cout << "Error receiving message" << std::endl;
}